package com.astrob.view;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astrob.NavFrameSDK;
import com.astrob.model.LonLat;
import com.astrob.nhcmap.R;
import com.astrob.util.Utils;

public class TipInfoView extends LinearLayout {

	private TextView mTipcar = null;
	private TextView mTipname = null;
	private TextView mTipman = null;

	private NavFrameSDK.POIDescription mPOIData = null;

	public TipInfoView(Context context) {
		super(context);
		init();
	}

	public TipInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		LayoutInflater inflate = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflate.inflate(R.layout.view_tipinfoview, this, true);

		mTipcar = (TextView) findViewById(R.id.btnTipcar);
		mTipname = (TextView) findViewById(R.id.btnTipname);
		mTipman = (TextView) findViewById(R.id.btnTipman);

		mTipname.setText("sdfeefe");
	}

	public void setOnClickListener(@Nullable OnClickListener l) {
		mTipcar.setOnClickListener(l);
		mTipname.setOnClickListener(l);
		mTipman.setOnClickListener(l);
	}

	public void setData(NavFrameSDK.POIDescription data) {
		mPOIData = data;
		if (data != null) {
			LonLat tipLL = Utils.gcj02towgs84(data.position.lon, data.position.lat);
			mPOIData.position.lon = tipLL.GetLon();
			mPOIData.position.lat = tipLL.GetLat();
			setText(data.name);
		}
	}

	public NavFrameSDK.POIDescription getData() {
		return mPOIData;
	}

	public void setLL(LonLat ll) {
		if (ll == null) {
			return;
		}
		if (mPOIData == null) {
			mPOIData = NavFrameSDK.getInstance().new POIDescription();
		}
		mPOIData.position.lon = ll.GetLon();
		mPOIData.position.lat = ll.GetLat();
	}

	//这里也是存GPS原始坐标
	public LonLat getLL() {
		if (mPOIData == null) {
			return null;
		}
		LonLat ll = new LonLat(mPOIData.position.lon, mPOIData.position.lat);
		if (ll.IsInvalid()) {
			return null;
		}
		return ll;
	}

	public Point tipPoint;

	public void setText(String name) {
		if (name.length() < 2) {
			setVisibility(View.GONE);
			return;
		}
		
		setVisibility(View.VISIBLE);
		if (name.length() > 20) {
			//name = name.substring(0, 20) + "...";
		}	

		mTipname.setText(name);
	}

	public void hideBtn()
	{
		mTipcar.setVisibility(View.GONE);
		mTipman.setVisibility(View.GONE);
	}

	public void showBtn()
	{
		mTipcar.setVisibility(View.VISIBLE);
		mTipman.setVisibility(View.VISIBLE);
	}

    public void showBtnR()
    {
        mTipcar.setVisibility(View.GONE);
        mTipman.setVisibility(View.VISIBLE);
    }
}
